package com.ccb.ic.bbs.controller;

import com.ccb.ic.bbs.enums.CommentTypeEnum;
import com.ccb.ic.query.bbs.CommentDTO;
import com.ccb.ic.query.bbs.QuestionDTO;
import com.ccb.ic.query.bbs.ResultDTO;
import com.ccb.ic.bbs.provider.UserProvider;
import com.ccb.ic.bbs.service.CommentService;
import com.ccb.ic.bbs.service.QuestionService;
import com.ccb.ic.domain.bbs.User;
import com.ccb.ic.domain.bbs.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private Environment env;
    @Autowired
    UserProvider userProvider;

    @Value("${vaptcha.vid}")
    private String vaptcha_vid;


    @GetMapping("/bbs/question/{id}")
    public Object question(@PathVariable(name = "id") Long id) {
        QuestionDTO questionDTO = questionService.getById(id);
        List<QuestionDTO> relatedQuestions = questionService.selectRelated(questionDTO);
        List<CommentDTO> comments = commentService.listByTargetId(id, CommentTypeEnum.QUESTION);
        //累加阅读数
        questionService.incView(id);
        Map<String, Object> model = new HashMap<>();
        model.put("question", questionDTO);
        model.put("comments", comments);
        model.put("relatedQuestions", relatedQuestions);
        model.put("navtype", "communitynav");
        //return "question";
        return ResultDTO.okOf(model);
    }


    @GetMapping("/bbs/p/{id}")
    public Object po(@PathVariable(name = "id") Long id, HttpServletRequest request) {
        QuestionDTO questionDTO = questionService.getById(id);
        User user = userProvider.getUser("token");
        UserAccount userAccount = userProvider.getUserAccount(user);
        Map<String, Object> model = new HashMap<>();
        if (userAccount == null) {
            if (questionDTO.getPermission() != 0) {
                model.put("rsTitle", "您无权访问！");
                model.put("rsMessage", "该贴设置了权限，游客不可见，快去登录吧");
                return ResultDTO.errorOf(model);
            }
        } else if (questionDTO.getPermission() > userAccount.getGroupId() && questionDTO.getCreator() != userAccount.getUserId()) {
            model.put("rsTitle", "您无权访问！");
            model.put("rsMessage", "该贴仅作者和" + env.getProperty("bbs.user.group.r" + questionDTO.getPermission()) + "及以上的用户可以访问，快去多多发帖提升等级或者开通VIP畅享全站！");
            return ResultDTO.errorOf(model);
        }
        List<QuestionDTO> relatedQuestions = questionService.selectRelated(questionDTO);
        List<CommentDTO> comments = commentService.listByTargetId(id, CommentTypeEnum.QUESTION);
        //累加阅读数
        questionService.incView(id);
        //这里提取简短描述
        String description = questionDTO.getDescription();
        String textDescription = description.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
        textDescription = textDescription.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回车,换行符,制表符
        textDescription = textDescription.replaceAll("&nbsp;", "");//去除&nbsp;
        if (textDescription.length() > 100) textDescription = textDescription.substring(0, 100);
        //System.out.println(textDescription);
        model.put("textDescription", questionDTO.getTitle() + ".." + textDescription);
        model.put("question", questionDTO);
        model.put("comments", comments);
        model.put("relatedQuestions", relatedQuestions);
        model.put("navtype", "communitynav");
        model.put("vaptcha_vid", vaptcha_vid);
        return ResultDTO.okOf(model);
    }


}
